package com.dhecp.project.flowable.controller;

import com.dhecp.common.core.controller.BaseController;
import com.dhecp.project.flowable.service.ActProcessDefinitionService;
import org.flowable.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;

@Controller
@RequestMapping("/flowable/definition")
public class ActProcessDefinitionController extends BaseController {

    private String prefix = "flowable/definition";

    @Autowired
    private ActProcessDefinitionService actProcessDefinitionService;

    @RequestMapping(value = "/getFlowsheetByKey/{procDefKey}", method = RequestMethod.GET)
    public void getFlowsheetByKey(@PathVariable String procDefKey,
                                     HttpServletResponse response) throws Exception{
        ProcessDefinition processDefinition = actProcessDefinitionService.queryActProcessDefinitionByKey(procDefKey);
        if(processDefinition == null){
            return;
        }
        String resourceName= processDefinition.getDiagramResourceName();
        InputStream inputStream = actProcessDefinitionService.getProcessDefinitionImage(processDefinition.getId(), processDefinition.getDeploymentId(), resourceName);
        if (inputStream != null) {
            // 设置页面不缓存
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setContentType("image/png");
            OutputStream os = response.getOutputStream();
            int bytesRead = 0;
            byte[] buffer = new byte[1024];
            while ((bytesRead = inputStream.read(buffer, 0, 1024)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            inputStream.close();
        }
    }

    @RequestMapping(value = "/getFlowsheetById/{proDefId}", method = RequestMethod.GET)
    public void getFlowsheetById(@PathVariable String proDefId,
                                     HttpServletResponse response) throws Exception{
        ProcessDefinition processDefinition = actProcessDefinitionService.queryActProcessDefinitionById(proDefId);
        if(processDefinition == null){
            return;
        }
        String resourceName= processDefinition.getDiagramResourceName();
        InputStream inputStream = actProcessDefinitionService.getProcessDefinitionImage(processDefinition.getId(), processDefinition.getDeploymentId(), resourceName);
        if (inputStream != null) {
            // 设置页面不缓存
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setContentType("image/png");
            OutputStream os = response.getOutputStream();
            int bytesRead = 0;
            byte[] buffer = new byte[1024];
            while ((bytesRead = inputStream.read(buffer, 0, 1024)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            inputStream.close();
        }
    }
}
